package com.chat.utils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {
	public static String getParm(String key) {
		Class<PropertiesUtil>	clazz	= PropertiesUtil.class;
		String					sRet	= "";
		Properties				prop	= new Properties();
        InputStream				in		= clazz.getResourceAsStream("/config.properties");
        try {
            prop.load(in);
            sRet = prop.getProperty(key).trim();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sRet;
	}
	public static int getIntParm(String key) {
		Class<PropertiesUtil>	clazz	= PropertiesUtil.class;
		String					sRet	= "";
		Properties				prop	= new Properties();
        InputStream				in		= clazz.getResourceAsStream("/config.properties");
        try {
            prop.load(in);
            sRet = prop.getProperty(key).trim();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int iRet = StringUtil.isInteger(sRet) ? Integer.valueOf(sRet) : 0;
        return iRet;
	}
	public static void setParm(String key, String val) {
        try {
    		Properties	prop	= new Properties();
    		File		file	= new File("config.properties");
        	InputStream in		= null;
        	in = new BufferedInputStream(new FileInputStream(getFilePath() + file));
        	prop.load(in);
        	FileOutputStream oFile = new FileOutputStream(getFilePath() + file);
        	prop.put("test", "xiaoheshan");
        	System.out.println("得到属性key:" + prop.getProperty("test"));
        	prop.store(oFile, "Modify properties.");
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
	public static String getFilePath() {
		String path=Thread.currentThread().getContextClassLoader().getResource("").toString();
		path=path.replace('/', '\\'); 
		path=path.replace("file:", ""); 
		path=path.replace("%20", " "); 
		path=path.substring(1);
		return path;
	}
}
